<?php

namespace app\models;

use Phalcon\Validation;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Email as EmailValidator;
use Phalcon\Mvc\Model\Behavior\Timestampable;

class User extends Base implements CanValidateInterface
{
    use CanValidateTrait;

    const POWER_ADMIN = 0;
    const POWER_USER = 1;
    /**
     *
     * @var integer
     * @Primary
     * @Identity
     * @Column(column="id", type="integer", length=10, nullable=false)
     * @Note("id")
     */
    protected $id;

    /**
     *
     * @var string
     * @Column(column="username", type="string", length=255, nullable=false)
     * @Note("用户名")
     */
    protected $username;

    /**
     *
     * @var string
     * @Column(column="password", type="string", length=255, nullable=false)
     * @Note("密码")
     */
    protected $password;

    /**
     *
     * @var string
     * @Column(column="name", type="string", length=255, nullable=false)
     * @Note("姓名")
     */
    protected $name;

    /**
     *
     * @var string
     * @Column(column="mobile", type="string", length=255, nullable=false)
     * @Note("手机号")
     */
    protected $mobile;

    /**
     *
     * @var string
     * @Column(column="phone", type="string", length=50, nullable=false)
     * @Note("电话")
     * @Input(type="text")
     */
    protected $phone;

    /**
     *
     * @var string
     * @Column(column="email", type="string", length=255, nullable=false)
     * @Note("邮箱")
     */
    protected $email;

    /**
     *
     * @var string
     * @Column(column="sex", type="string", length=255, nullable=false)
     * @Note("性别")
     */
    protected $sex;

    /**
     *
     * @var string
     * @Column(column="duty", type="string", length=255, nullable=false)
     * @Note("职务")
     */
    protected $duty;

    /**
     *
     * @var integer
     * @Column(column="power", type="integer", length=4, nullable=false)
     * @Note("权限")
     */
    protected $power;
    /**
     *
     * @var integer
     * @Column(column="company_id", type="integer", length=11, nullable=false)
     * @Note("公司")
     */
    protected $company_id;
    /**
     *
     * @var integer
     * @Column(column="depart_id", type="integer", length=11, nullable=false)
     * @Note("部门")
     */
    protected $depart_id;

    /**
     *
     * @var integer
     * @Column(column="role_id", type="integer", length=11, nullable=false)
     * @Note("角色")
     */
    protected $role_id;

    /**
     *
     * @var integer
     * @Column(column="on_job", type="integer", length=4, nullable=false)
     * @Note("是否在职")
     */
    protected $on_job;

    /**
     *
     * @var integer
     * @Column(column="phone_type", type="integer", length=11, nullable=false)
     * @Note("在线类型")
     */
    protected $phone_type;

    /**
     *
     * @var integer
     * @Column(column="created_at", type="integer", length=11, nullable=false)
     * @Note("创建时间")
     */
    protected $created_at;

    /**
     *
     * @var integer
     * @Column(column="updated_at", type="integer", length=11, nullable=false)
     * @Note("修改时间")
     */
    protected $updated_at;

    /**
     *
     * @var integer
     * @Column(column="start_at", type="integer", length=11, nullable=false)
     * @Note("参加工作时间")
     */
    protected $start_at;

    /**
     *
     * @var integer
     * @Column(column="unit_start_at", type="integer", length=11, nullable=false)
     * @Note("本单位工作时间")
     */
    protected $unit_start_at;

    /**
     * Method to set the value of field id
     *
     * @param integer $id
     * @return $this
     */
    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    /**
     * Method to set the value of field username
     *
     * @param string $username
     * @return $this
     */
    public function setUsername($username)
    {
        $this->username = $username;

        return $this;
    }

    /**
     * Method to set the value of field password
     *
     * @param string $password
     * @return $this
     */
    public function setPassword($password)
    {
        $this->password = $password;

        return $this;
    }

    /**
     * Method to set the value of field name
     *
     * @param string $name
     * @return $this
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Method to set the value of field mobile
     *
     * @param string $mobile
     * @return $this
     */
    public function setMobile($mobile)
    {
        $this->mobile = $mobile;

        return $this;
    }

    /**
     * Method to set the value of field phone
     *
     * @param string $phone
     * @return $this
     */
    public function setPhone($phone)
    {
        $this->phone = $phone;

        return $this;
    }

    /**
     * Method to set the value of field email
     *
     * @param string $email
     * @return $this
     */
    public function setEmail($email)
    {
        $this->email = $email;

        return $this;
    }

    /**
     * Method to set the value of field sex
     *
     * @param string $sex
     * @return $this
     */
    public function setSex($sex)
    {
        $this->sex = $sex;

        return $this;
    }

    /**
     * Method to set the value of field duty
     *
     * @param string $duty
     * @return $this
     */
    public function setDuty($duty)
    {
        $this->duty = $duty;

        return $this;
    }

    /**
     * Method to set the value of field power
     *
     * @param integer $power
     * @return $this
     */
    public function setPower($power)
    {
        $this->power = $power;

        return $this;
    }

    public function setCompanyId($company_id)
    {
        $this->company_id = $company_id;
        return $this;
    }

    /**
     * Method to set the value of field depart_id
     *
     * @param integer $depart_id
     * @return $this
     */
    public function setDepartId($depart_id)
    {
        $this->depart_id = $depart_id;

        return $this;
    }

    /**
     * Method to set the value of field role_id
     *
     * @param integer $role_id
     * @return $this
     */
    public function setRoleId($role_id)
    {
        $this->role_id = $role_id;

        return $this;
    }

    /**
     * Method to set the value of field on_job
     *
     * @param integer $on_job
     * @return $this
     */
    public function setOnJob($on_job)
    {
        $this->on_job = $on_job;

        return $this;
    }

    /**
     * Method to set the value of field phone_type
     *
     * @param integer $phone_type
     * @return $this
     */
    public function setPhoneType($phone_type)
    {
        $this->phone_type = $phone_type;

        return $this;
    }

    /**
     * Method to set the value of field start_at
     *
     * @param integer $start_at
     * @return $this
     */
    public function setStartAt($start_at)
    {
        $this->start_at = $start_at;

        return $this;
    }

    /**
     * Method to set the value of field unit_start_at
     *
     * @param integer $unit_start_at
     * @return $this
     */
    public function setUnitStartAt($unit_start_at)
    {
        $this->unit_start_at = $unit_start_at;

        return $this;
    }

    /**
     * Returns the value of field id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Returns the value of field username
     *
     * @return string
     */
    public function getUsername()
    {
        return $this->username;
    }

    /**
     * Returns the value of field password
     *
     * @return string
     */
    public function getPassword()
    {
        return $this->password;
    }

    /**
     * Returns the value of field name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * Returns the value of field mobile
     *
     * @return string
     */
    public function getMobile()
    {
        return $this->mobile;
    }

    /**
     * Returns the value of field phone
     *
     * @return string
     */
    public function getPhone()
    {
        return $this->phone;
    }

    /**
     * Returns the value of field email
     *
     * @return string
     */
    public function getEmail()
    {
        return $this->email;
    }

    /**
     * Returns the value of field sex
     *
     * @return string
     */
    public function getSex()
    {
        return $this->sex;
    }

    /**
     * Returns the value of field duty
     *
     * @return string
     */
    public function getDuty()
    {
        return $this->duty;
    }

    /**
     * Returns the value of field power
     *
     * @return integer
     */
    public function getPower()
    {
        return $this->power;
    }

    /**
     * Returns the value of field depart_id
     *
     * @return integer
     */
    public function getCompanyId()
    {
        return $this->company_id;
    }

    /**
     * Returns the value of field depart_id
     *
     * @return integer
     */
    public function getDepartId()
    {
        return $this->depart_id;
    }

    /**
     * Returns the value of field role_id
     *
     * @return integer
     */
    public function getRoleId()
    {
        return $this->role_id;
    }

    /**
     * Returns the value of field on_job
     *
     * @return integer
     */
    public function getOnJob()
    {
        return $this->on_job;
    }

    /**
     * Returns the value of field phone_type
     *
     * @return integer
     */
    public function getPhoneType()
    {
        return $this->phone_type;
    }

    /**
     * Returns the value of field created_at
     *
     * @return integer
     */
    public function getCreatedAt()
    {
        return $this->created_at;
    }

    /**
     * Returns the value of field updated_at
     *
     * @return integer
     */
    public function getUpdatedAt()
    {
        return $this->updated_at;
    }

    /**
     * Returns the value of field start_at
     *
     * @return integer
     */
    public function getStartAt()
    {
        return $this->start_at;
    }

    /**
     * Returns the value of field unit_start_at
     *
     * @return integer
     */
    public function getUnitStartAt()
    {
        return $this->unit_start_at;
    }


    public function prepareValidation()
    {
        $validator = new Validation();

        $validator->rules('email',[
            new PresenceOf(
                [
                    'message'=>'邮箱不能为空','cancelOnFail'=>true
                ]
            ),
            new EmailValidator(
                [
                    'model' => $this,
                    'message' => '清输入正确的邮箱',
                ]
            )
        ]);

        $validator->rules('username',[
            new PresenceOf(
                [
                    'message'=>'用户名称不能为空','cancelOnFail'=>true
                ]
            ),
        ]);

        $validator->rules('password',[
            new PresenceOf(
                [
                    'message'=>'密码不能为空','cancelOnFail'=>true
                ]
            ),
        ]);

        return $validator;
    }

    /**
     * Initialize method for model.
     */
    public function initialize()
    {
        $this->setSchema("insmanage");
        $this->setSource("user");
        $this->addBehavior(
            new Timestampable(
                [
                    'beforeCreate' => [
                        'field' => ['created_at','updated_at'],
                    ],
                    'beforeUpdate'=>[
                        'field'=>'updated_at'
                    ]
                ]
            )
        );
        $this->belongsTo('company_id',Company::class,"id");
    }

    /**
     * Returns table name mapped in the model.
     *
     * @return string
     */
    public function getSource()
    {
        return 'user';
    }

    /**
     * Allows to query a set of records that match the specified conditions
     *
     * @param mixed $parameters
     * @return User[]|User|\Phalcon\Mvc\Model\ResultSetInterface
     */
    public static function find($parameters = null)
    {
        return parent::find($parameters);
    }

    /**
     * Allows to query the first record that match the specified conditions
     *
     * @param mixed $parameters
     * @return User|\Phalcon\Mvc\Model\ResultInterface
     */
    public static function findFirst($parameters = null)
    {
        return parent::findFirst($parameters);
    }

    /**
     * 模型事件
     */
    public function beforeCreate()
    {
        if(!$this->company_id) {//通过注册生成的,生成管理员
            $company = new Company();
            $company->create();
            $this->company_id = $company->id;
            $this->power = self::POWER_ADMIN;
        }else{//生成用户
            $this->power = self::POWER_USER;
        }
    }
}
